<?php
namespace Admin\Controller;

use Admin\Controller\GlobalController;

class IndexController extends GlobalController
{
    public function index()
    {
        $admin_uid = session('ADMIN_UID');
        //获取一级导航菜单
        $modelObj = new \Admin\Model\AdminMenuModel();
        $navList = $modelObj->where('tier_id=1')->select();
        $this->assign('navList', $navList);

        //获取一级菜单class_id
        $class_id = I('get.class_id', '01', 'trim');
        $this->sideMenu($admin_uid, $class_id);
        $this->assign('class_id', $class_id);
        $this->display();
    }

    public function upload()
    {
        header("Content-Type: text/html; charset=utf-8");
        $action = I('get.action');

        switch ($action) {
            case 'config':
                $config_arr = include_once APP_PATH . 'Admin/Util/Uploader.config.php';
                $imageActionName = I('get.imageActionName');
                if ($imageActionName) $config_arr['imageActionName'] = $imageActionName;
                $result = json_encode($config_arr);
                break;
            /* 自拍墙封面图片 */
            case 'upload_material':
                $up = new \Admin\Util\Uploader();
                $up->upFile('upfile');
                $fileInfo = $up->getFileInfo();
                $up->imkThumb($fileInfo['url'], 200, 0, 'ratable');
                $up->imkThumb($fileInfo['url'], 300, 0, 'ratable');

                $result = json_encode($fileInfo);
                break;
            /* 剧透的图片 */
            case 'upload_spoiler':
                $up = new \Admin\Util\Uploader();
                $up->upFile('upfile');
                $fileInfo = $up->getFileInfo();
                $up->imkThumb($fileInfo['url'], 200, 0, 'ratable');
                $up->imkThumb($fileInfo['url'], 300, 0, 'ratable');

                $result = json_encode($fileInfo);
                break;
            /* 音频小图 */
            case 'upload_sound_img':
                $up = new \Admin\Util\Uploader();
                $up->upFile('upfile');
                $fileInfo = $up->getFileInfo();
                $up->imkThumb($fileInfo['url'], 262, 262, 'fixed');
                $up->imkThumb($fileInfo['url'], 200, 200, 'fixed');

                $result = json_encode($fileInfo);
                break;
            /* APP需要的图片 */
            case 'upload_caidan':
                $up = new \Admin\Util\Uploader();
                $up->upFile('upfile');
                $fileInfo = $up->getFileInfo();
                $up->imkThumb($fileInfo['url'], 450, 0, 'ratable');
                $up->imkThumb($fileInfo['url'], 650, 0, 'ratable');

                $result = json_encode($fileInfo);
                break;
            // 编辑器内用到的图片
            case 'uploadimage':
                $up = new \Admin\Util\Uploader();
                $up->upFile('upfile');
                $fileInfo = $up->getFileInfo();
                $up->imkThumb($fileInfo['url'], 450, 0, 'ratable');
                $up->imkThumb($fileInfo['url'], 650, 0, 'ratable');

                //大于450的图直接用450的缩略图
                preg_match("/\.(\d+)x(\d+)\./", $fileInfo['url'], $match);
                if (!empty($match[1]) && $match[1] >= 450) {
                    $fileInfo['url'] = getRatableImg($fileInfo['url'], 450);
                }

                $result = json_encode($fileInfo);
                break;
            /* 上传文件 */
            case 'uploadfile':
                $up = new \Admin\Util\Uploader();
                $up->upFile('upfile');
                $fileInfo = $up->getFileInfo();

                $dir_prefix = WEB_PATH . '/www';
                //如果为音频，则获取它的播放时长信息
                if (strrchr($fileInfo['url'], ".mp3") == '.mp3') {
                    require_once APP_PATH . 'Common/Util/getid3/getid3.php';
                    $getID3 = new \getID3();
                    $ThisFileInfo = $getID3->analyze($dir_prefix . $fileInfo['url']);
                    $duration = round($ThisFileInfo['playtime_seconds']);
                    $fileInfo['duration'] = $duration;
                }

                $result = json_encode($fileInfo);
                break;
            // 编辑器保存远程图片
            case 'catchimage':
                $sourceArr = I('post.source', array());
                $fileInfoArr = array();
                $up = new \Admin\Util\Uploader();
                foreach ($sourceArr as $source) {
                    $up->saveRemote($source);
                    $fileInfo = $up->getFileInfo();
                    //微信的图片地址转码问题
                    $source = str_replace('&amp;', '&', $source);
                    $fileInfo['source'] = $source;
                    $up->imkThumb($fileInfo['url'], 450, 0, 'ratable');
                    $up->imkThumb($fileInfo['url'], 650, 0, 'ratable');

                    //大于450的图直接用450的缩略图
                    preg_match("/\.(\d+)x(\d+)\./", $fileInfo['url'], $match);
                    if (!empty($match[1]) && $match[1] >= 450) {
                        $fileInfo['url'] = getRatableImg($fileInfo['url'], 450);
                    }
                    $fileInfoArr[] = $fileInfo;
                }

                $result = json_encode(array(
                    'status' => 'SUCCESS',
                    'list' => $fileInfoArr,
                ));
                break;
            default:
                $result = json_encode(array(
                    'state' => '请求地址出错'
                ));
                break;
        }
        /* 输出结果 */
        if (isset($_GET["callback"])) {
            if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
                echo htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
            } else {
                echo json_encode(array(
                    'state' => 'callback参数不合法'
                ));
            }
        } else {
            echo $result;
        }
    }

    private function sideMenu($uid, $class_id)
    {
        $myAtomList = array();
        $classArray = array();
        #取该用户的权限列表
        $userAdminObj = new \Admin\Model\AdminModel();
        $atomArray = $userAdminObj->getAtomArray4id($uid, $class_id);
        $permissionAtomObj = new \Admin\Model\AdminAtomModel();
        foreach ($atomArray as $key => $value) {
            //通过权限id，查出可以显示的class_id
            $classArray = array_merge($classArray, $permissionAtomObj->getClassArray4id($key, $class_id));
            $atomInfo = $permissionAtomObj->getPermissionAtomInfo($key);
            //取权限详情并按照class_id过滤
            if (substr($atomInfo['menu_main_class'], 0, strlen($class_id)) == $class_id) {
                $myAtomList[$value['menu_main_class']][] = $atomInfo;
            }
        }
        //获取目录列表
        $menuMainObj = new \Admin\Model\AdminMenuModel();
        $menuList = $menuMainObj->getMenuTree4ClassId($class_id, 10);
        $menuMain = array_shift($menuList);
        $this->assign('myAtomList', $myAtomList);
        $this->assign('menuShow', $classArray);
        $this->assign('menuMain', $menuMain);
        $this->assign('menuSub', $menuList);
    }

    public function signsina() {
        $app = C('WEIBO_OAUTH');
        include_once (APP_PATH . "/Common/Util/sina/saetv2.ex.class.php");
        $o = new \SaeTOAuthV2($app['key'], $app['secret']);
        $callback = "http://{$_SERVER['HTTP_HOST']}/admin/index/callback_sina";
        $code_url = $o->getAuthorizeURL($callback);
        header('Location:' . $code_url);
   	}

    public function callback_sina() {
        if (empty($_REQUEST['code'])) exit();

        $app = C('WEIBO_OAUTH');
        include_once (APP_PATH . "/Common/Util/sina/saetv2.ex.class.php");
        $o = new \SaeTOAuthV2($app['key'], $app['secret']);
        $callback = "http://{$_SERVER['HTTP_HOST']}/admin/index/callback_sina";

        $keys = array();
        $keys['code'] = $_REQUEST['code'];
        $keys['redirect_uri'] = $callback;
        try {
            $token = $o->getAccessToken('code', $keys);
        } catch (\Exception $e) {
        }

        if ($token) {
            $sModel = M('site_setting');
            $sModel->where(array('site_key' => 'weibo_access_token'))->setField('site_value', $token['access_token']);
            exit('success');
        } else {
            exit('error');
        }

        $rs = $c->upload("好漫画 - 《教你画漫画》-漫画速成人物篇http://caidan.manyule.com/mobile/share/news_id/1010os", 'http://image.manyule.com/caidan/uploads/image/20150108/1420685099808142.833x1600.jpg.450x864.jpg');

        var_dump($rs);
    }
}